﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DatabaseHelper;
using System.IO;

namespace UI.Online.SubForms
{
    public partial class frmExportHdd : Form
    {
        public string NewProjectLocation;
        public string NewProjectName;
        public frmOnlineUI2 fM = null;
        string DatabaseFilePath;
        string DatabaseFileName;
        string PipelineRecorderFolder;
        string PipelineReviewFolder;
        int NewProjectId;

        public frmExportHdd()
        {
            InitializeComponent();
        }

        public frmExportHdd(Form callingForm)
            : this()
        {
            fM = callingForm as frmOnlineUI2;
            PipelineRecorderFolder = Directory.GetCurrentDirectory();
            PipelineReviewFolder = PipelineRecorderFolder.Substring(0, PipelineRecorderFolder.Length - 21) + "CNA Pipeline Review";
            DatabaseFileName = "dbCNA.mdb";
            DatabaseFilePath = PipelineRecorderFolder + "\\" + DatabaseFileName;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            // Show the FolderBrowserDialog.
            DialogResult result = fbdLocation.ShowDialog();
            if (result == DialogResult.OK)
            {
                NewProjectLocation = fbdLocation.SelectedPath;
                txtProjectLocation.Text = NewProjectLocation;
            }
        }

        private void frmExportHdd_Load(object sender, EventArgs e)
        {
            cbbProjectName.Items.Clear();
            DBHelper dh = new DBHelper();
            dh.Query = "SELECT * FROM Projects";
            DataTable dt = dh.Fill();
            foreach (DataRow dr in dt.Rows)
            {
                string name = dr["ProjectName"].ToString();
                cbbProjectName.Items.Add(name);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (cbbProjectName.SelectedIndex != -1)
            {
                if (NewProjectLocation != null)
                {
                    if (!NewProjectLocation.EndsWith("\\"))
                    {
                        NewProjectLocation += "\\";
                    }
                    string ReviewSoftwareFolder = NewProjectLocation + "\\CNA Pipeline Review";
                    DirectoryCopy(PipelineReviewFolder, ReviewSoftwareFolder, true);
                    String NewDatabaseFilePath = ReviewSoftwareFolder + "\\" + DatabaseFileName;
                    File.Copy(DatabaseFilePath, NewDatabaseFilePath, true);
                    DBHelper dh = new DBHelper(NewDatabaseFilePath);
                    dh.DeleteProjectsExcept(NewProjectName);
                    if (NewProjectId > 0)
                    {
                        dh.UpdateProjectLocation(NewProjectId, NewProjectLocation);
                    }
                    else
                    {
                        MessageBox.Show(
                            NewProjectName + " is not exsist.\nPlease select again.",
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                    }
                }
            }
        }

        private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirName);
            }

            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                file.CopyTo(temppath, true);
            }

            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }

        private void cbbProjectName_SelectedIndexChanged(object sender, EventArgs e)
        {
            NewProjectName = cbbProjectName.Text;
            DBHelper dh = new DBHelper();
            NewProjectId = dh.FindProject(NewProjectName);
        }
    }
}
